<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222" media="(prefers-color-scheme: light)">
<meta name="theme-color" content="#222" media="(prefers-color-scheme: dark)"><meta name="generator" content="Hexo 5.2.0">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.1.2/css/all.min.css" integrity="sha256-xejo6yLi6vGtAjcMIsY8BHdKsLg7QynVlFMzdQgUuy8=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/fancybox/3.5.7/jquery.fancybox.min.css" integrity="sha256-Vzbj7sDDS/woiFS3uNKo8eIuni59rjyNGtXfstRzStA=" crossorigin="anonymous">

<script class="next-config" data-name="main" type="application/json">{"hostname":"gz1234.gitee.io","root":"/","images":"/images","scheme":"Gemini","darkmode":true,"version":"8.12.3","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":{"enable":true,"style":"flat"},"bookmark":{"enable":true,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"},"path":"/search.xml","localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false}}</script><script src="/js/config.js"></script>

    <meta name="description" content="什么是Buffer 缓冲区Buffer是暂时存放输入输出数据的一段内存。 JS语言没有二进制数据类型，而在处理TCP和文件流的时候，必须要处理二进制数据。 NodeJS提供了一个Buffer对象来提供对二进制数据的操作 是一个表示固定内存分配的全局对象，也就是说要放到缓存区中的字节数需要提前确定 Buffer好比由一个8位字节元素组成的数组，可以有效的在JavasScript中表示二进制数据  什">
<meta property="og:type" content="article">
<meta property="og:title" content="Buffer">
<meta property="og:url" content="https://gz1234.gitee.io/2022/07/09/%E5%89%8D%E7%AB%AF%E8%AE%B0%E5%BD%95%E6%95%B4%E7%90%86/Buffer/index.html">
<meta property="og:site_name" content="郭泽">
<meta property="og:description" content="什么是Buffer 缓冲区Buffer是暂时存放输入输出数据的一段内存。 JS语言没有二进制数据类型，而在处理TCP和文件流的时候，必须要处理二进制数据。 NodeJS提供了一个Buffer对象来提供对二进制数据的操作 是一个表示固定内存分配的全局对象，也就是说要放到缓存区中的字节数需要提前确定 Buffer好比由一个8位字节元素组成的数组，可以有效的在JavasScript中表示二进制数据  什">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2022-07-08T19:21:52.000Z">
<meta property="article:modified_time" content="2022-11-16T02:09:11.879Z">
<meta property="article:author" content="郭泽">
<meta property="article:tag" content="Buffer">
<meta name="twitter:card" content="summary">


<link rel="canonical" href="https://gz1234.gitee.io/2022/07/09/%E5%89%8D%E7%AB%AF%E8%AE%B0%E5%BD%95%E6%95%B4%E7%90%86/Buffer/">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"https://gz1234.gitee.io/2022/07/09/%E5%89%8D%E7%AB%AF%E8%AE%B0%E5%BD%95%E6%95%B4%E7%90%86/Buffer/","path":"2022/07/09/前端记录整理/Buffer/","title":"Buffer"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>Buffer | 郭泽</title>
  





  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <p class="site-title">郭泽</p>
      <i class="logo-line"></i>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu"><li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a></li><li class="menu-item menu-item-about"><a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a></li><li class="menu-item menu-item-tags"><a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a></li><li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a></li><li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup"><div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off" maxlength="80"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close" role="button">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="search-result-container no-result">
  <div class="search-result-icon">
    <i class="fa fa-spinner fa-pulse fa-5x"></i>
  </div>
</div>

    </div>
  </div>

</div>
        
  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%BB%80%E4%B9%88%E6%98%AFBuffer"><span class="nav-number">1.</span> <span class="nav-text">什么是Buffer</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%BB%80%E4%B9%88%E6%98%AF%E5%AD%97%E8%8A%82"><span class="nav-number">2.</span> <span class="nav-text">什么是字节</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E8%BF%9B%E5%88%B6"><span class="nav-number">3.</span> <span class="nav-text">进制</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%AE%9A%E4%B9%89buffer%E7%9A%84%E4%B8%89%E7%A7%8D%E6%96%B9%E5%BC%8F"><span class="nav-number">4.</span> <span class="nav-text">定义buffer的三种方式</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#buffer%E5%B8%B8%E7%94%A8%E6%96%B9%E6%B3%95"><span class="nav-number">5.</span> <span class="nav-text">buffer常用方法</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#base64"><span class="nav-number">6.</span> <span class="nav-text">base64</span></a></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">郭泽</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap site-overview-item animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">54</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">21</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
          <a href="/tags/">
        <span class="site-state-item-count">51</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>



        </div>
      </div>
    </div>
  </aside>
  <div class="sidebar-dimmer"></div>


    </header>

    
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>
  <a role="button" class="book-mark-link book-mark-link-fixed"></a>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://gz1234.gitee.io/2022/07/09/%E5%89%8D%E7%AB%AF%E8%AE%B0%E5%BD%95%E6%95%B4%E7%90%86/Buffer/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="郭泽">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="郭泽">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
      <meta itemprop="name" content="Buffer | 郭泽">
      <meta itemprop="description" content="">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          Buffer
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2022-07-09 03:21:52" itemprop="dateCreated datePublished" datetime="2022-07-09T03:21:52+08:00">2022-07-09</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar-check"></i>
      </span>
      <span class="post-meta-item-text">更新于</span>
      <time title="修改时间：2022-11-16 10:09:11" itemprop="dateModified" datetime="2022-11-16T10:09:11+08:00">2022-11-16</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E5%89%8D%E7%AB%AF%E6%95%B4%E7%90%86/" itemprop="url" rel="index"><span itemprop="name">前端整理</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <h2 id="什么是Buffer"><a href="#什么是Buffer" class="headerlink" title="什么是Buffer"></a>什么是Buffer</h2><ul>
<li>缓冲区Buffer是暂时存放输入输出数据的一段内存。</li>
<li>JS语言没有二进制数据类型，而在处理TCP和文件流的时候，必须要处理二进制数据。</li>
<li>NodeJS提供了一个Buffer对象来提供对二进制数据的操作</li>
<li>是一个表示固定内存分配的全局对象，也就是说要放到缓存区中的字节数需要提前确定</li>
<li>Buffer好比由一个8位字节元素组成的数组，可以有效的在JavasScript中表示二进制数据</li>
</ul>
<h2 id="什么是字节"><a href="#什么是字节" class="headerlink" title="什么是字节"></a>什么是字节</h2><ul>
<li>字节(Byte)是计算机存储时的一种计量单位，一个字节等于8位二进制数</li>
<li>一个位就代表一个0或1，每8个位（bit）组成一个字节（Byte）</li>
<li>字节是通过网络传输信息的基本单位</li>
<li>一个字节最大值十进制数是255(2**8-1)</li>
</ul>
<h2 id="进制"><a href="#进制" class="headerlink" title="进制"></a>进制</h2><ul>
<li><p>0b 2进制</p>
</li>
<li><p>0x 16进制</p>
</li>
<li><p>0o 8进制</p>
</li>
<li><p>将任意进制字符串转换为十进制</p>
<a id="more"></a>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">parseInt</span>(<span class="string">&quot;11&quot;</span>, <span class="number">2</span>);</span><br><span class="line"><span class="built_in">parseInt</span>(<span class="string">&quot;77&quot;</span>, <span class="number">8</span>);</span><br><span class="line"><span class="built_in">parseInt</span>(<span class="string">&quot;e7&quot;</span>, <span class="number">16</span>);</span><br></pre></td></tr></table></figure>
</li>
<li><p>将10进制转换为其它进制字符串</p>
</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">(<span class="number">3</span>).toString(<span class="number">2</span>)</span><br><span class="line">(<span class="number">17</span>).toString(<span class="number">16</span>)</span><br><span class="line">(<span class="number">33</span>).toString(<span class="number">32</span>)</span><br></pre></td></tr></table></figure>

<h2 id="定义buffer的三种方式"><a href="#定义buffer的三种方式" class="headerlink" title="定义buffer的三种方式"></a>定义buffer的三种方式</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="keyword">const</span> buf1 = Buffer.alloc(<span class="number">10</span>);</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> buf2 = Buffer.alloc(<span class="number">10</span>, <span class="number">1</span>);</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> buf3 = Buffer.allocUnsafe(<span class="number">10</span>);</span><br></pre></td></tr></table></figure>

<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="keyword">const</span> buf4 = Buffer.from([<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>]);</span><br></pre></td></tr></table></figure>

<blockquote>
<p>正常情况下为0-255之间;</p>
</blockquote>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> buf5 = Buffer.from(<span class="string">&#x27;珠峰培训&#x27;</span>);</span><br></pre></td></tr></table></figure>

<h2 id="buffer常用方法"><a href="#buffer常用方法" class="headerlink" title="buffer常用方法"></a>buffer常用方法</h2><p><code>buf.fill(value[, offset[, end]][, encoding])</code></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">buffer.fill(<span class="number">0</span>);</span><br></pre></td></tr></table></figure>

<p><code>buf.write(string[, offset[, length]][, encoding])</code></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> buffer = Buffer.allocUnsafe(<span class="number">6</span>);</span><br><span class="line">buffer.write(<span class="string">&#x27;珠&#x27;</span>,<span class="number">0</span>,<span class="number">3</span>,<span class="string">&#x27;utf8&#x27;</span>);</span><br><span class="line">buffer.write(<span class="string">&#x27;峰&#x27;</span>,<span class="number">3</span>,<span class="number">3</span>,<span class="string">&#x27;utf8&#x27;</span>);</span><br></pre></td></tr></table></figure>

<ul>
<li>通过指定的 <code>offset</code> 将 <code>value</code> 写入到当前 Buffer 中。</li>
<li>这个 value 应当是一个有效的有符号的8位整数</li>
</ul>
<p><code>buf.writeInt8(value, offset[, noAssert])</code></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> buf = Buffer.alloc(<span class="number">4</span>);</span><br><span class="line">buf.writeInt8()</span><br><span class="line">buf.writeInt8(<span class="number">0</span>,<span class="number">0</span>);</span><br><span class="line">buf.writeInt8(<span class="number">16</span>,<span class="number">1</span>);</span><br><span class="line">buf.writeInt8(<span class="number">32</span>,<span class="number">2</span>);</span><br><span class="line">buf.writeInt8(<span class="number">48</span>,<span class="number">3</span>);</span><br><span class="line"><span class="built_in">console</span>.log(buf);</span><br><span class="line"><span class="built_in">console</span>.log(buf.readInt8(<span class="number">0</span>));</span><br><span class="line"><span class="built_in">console</span>.log(buf.readInt8(<span class="number">1</span>));</span><br><span class="line"><span class="built_in">console</span>.log(buf.readInt8(<span class="number">2</span>));</span><br><span class="line"><span class="built_in">console</span>.log(buf.readInt8(<span class="number">3</span>));</span><br></pre></td></tr></table></figure>

<p>不同的CPU有不同的字节序类型，这些字节序是指整数在内存中保存的顺序。</p>
<ul>
<li>Big-endian：将高序字节存储在起始地址（高位编址）</li>
<li>Little-endian：将低序字节存储在起始地址（低位编址）</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> buffer = Buffer.alloc(<span class="number">4</span>);</span><br><span class="line">buffer.writeInt16BE(<span class="number">2</span>**<span class="number">8</span>,<span class="number">0</span>);</span><br><span class="line"><span class="built_in">console</span>.log(buffer);</span><br><span class="line"><span class="built_in">console</span>.log(buffer.readInt16BE(<span class="number">0</span>));</span><br><span class="line"></span><br><span class="line">buffer.writeInt16LE(<span class="number">2</span>**<span class="number">8</span>,<span class="number">2</span>);</span><br><span class="line"><span class="built_in">console</span>.log(buffer);</span><br><span class="line"><span class="built_in">console</span>.log(buffer.readInt16LE(<span class="number">2</span>));</span><br></pre></td></tr></table></figure>

<p><code>buf.toString([encoding[, start[, end]]])</code></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> buffer = Buffer.from(<span class="string">&#x27;珠峰架构&#x27;</span>);</span><br><span class="line"><span class="built_in">console</span>.log(buffer.toString(<span class="string">&#x27;utf8&#x27;</span>,<span class="number">3</span>,<span class="number">6</span>));</span><br></pre></td></tr></table></figure>

<p><code>buf.slice([start[, end]])</code></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> buffer = Buffer.from(<span class="string">&#x27;珠峰架构&#x27;</span>);</span><br><span class="line"><span class="keyword">let</span> subBuffer = buffer.slice(<span class="number">0</span>,<span class="number">6</span>);</span><br><span class="line"><span class="built_in">console</span>.log(subBuffer.toString());</span><br></pre></td></tr></table></figure>

<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> &#123;StringDecoder&#125;  = <span class="built_in">require</span>(<span class="string">&#x27;string_decoder&#x27;</span>);</span><br><span class="line"><span class="keyword">let</span> sd = <span class="keyword">new</span> StringDecoder();</span><br><span class="line"><span class="keyword">let</span> buffer = Buffer.from(<span class="string">&#x27;珠峰&#x27;</span>);</span><br><span class="line"><span class="built_in">console</span>.log(sd.write(buffer.slice(<span class="number">0</span>,<span class="number">4</span>)));</span><br><span class="line"><span class="built_in">console</span>.log(sd.write(buffer.slice(<span class="number">4</span>)));</span><br></pre></td></tr></table></figure>

<ul>
<li>复制Buffer 把多个buffer拷贝到一个大buffer上</li>
</ul>
<p><code>buf.copy(target[, targetStart[, sourceStart[, sourceEnd]]])</code></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> buffer = Buffer.from(<span class="string">&#x27;珠峰架构&#x27;</span>);</span><br><span class="line"><span class="keyword">let</span> subBuffer = Buffer.alloc(<span class="number">6</span>);</span><br><span class="line">buffer.copy(subBuffer,<span class="number">0</span>,<span class="number">0</span>,<span class="number">4</span>);</span><br><span class="line">buffer.copy(subBuffer,<span class="number">3</span>,<span class="number">3</span>,<span class="number">6</span>);</span><br><span class="line"><span class="built_in">console</span>.log(subBuffer.toString());</span><br></pre></td></tr></table></figure>

<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">Buffer.prototype.copy = <span class="function"><span class="keyword">function</span>(<span class="params">targetBuffer,targetStart,sourceStart,sourceEnd</span>)</span>&#123;</span><br><span class="line">    <span class="keyword">for</span>(<span class="keyword">let</span> i=sourceStart;ithis[i];</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">let</span> buffer = Buffer.from(<span class="string">&#x27;珠峰&#x27;</span>);</span><br><span class="line"><span class="keyword">let</span> subBuffer = Buffer.alloc(<span class="number">6</span>);</span><br><span class="line">buffer.copy(subBuffer,<span class="number">0</span>,<span class="number">0</span>,<span class="number">4</span>);</span><br><span class="line">buffer.copy(subBuffer,<span class="number">3</span>,<span class="number">3</span>,<span class="number">6</span>);</span><br><span class="line"><span class="built_in">console</span>.log(subBuffer.toString());</span><br></pre></td></tr></table></figure>

<p><code>Buffer.concat(list[, totalLength])</code></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> buffer1 = Buffer.from(<span class="string">&#x27;珠&#x27;</span>);</span><br><span class="line"><span class="keyword">let</span> buffer2 = Buffer.from(<span class="string">&#x27;峰&#x27;</span>);</span><br><span class="line"><span class="keyword">let</span> buffer = Buffer.concat([buffer1,buffer2]);</span><br><span class="line"><span class="built_in">console</span>.log(buffer.toString());</span><br></pre></td></tr></table></figure>

<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">Buffer.concat = <span class="function"><span class="keyword">function</span> (<span class="params">list</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">let</span> totalLength = list.reduce(<span class="function">(<span class="params">len, item</span>) =&gt;</span> len + item.length, <span class="number">0</span>);</span><br><span class="line">    <span class="keyword">if</span> (list.length == <span class="number">0</span>)</span><br><span class="line">        <span class="keyword">return</span> list[<span class="number">0</span>];</span><br><span class="line">    <span class="keyword">let</span> newBuffer = Buffer.alloc(totalLength);</span><br><span class="line">    <span class="keyword">let</span> pos = <span class="number">0</span>;</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">let</span> buffer <span class="keyword">of</span> list) &#123;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">let</span> byte <span class="keyword">of</span> buffer) &#123;</span><br><span class="line">            newBuffer[pos++] = byte;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> newBuffer;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">let</span> buffer1 = Buffer.from(<span class="string">&#x27;珠&#x27;</span>);</span><br><span class="line"><span class="keyword">let</span> buffer2 = Buffer.from(<span class="string">&#x27;峰&#x27;</span>);</span><br><span class="line"><span class="keyword">let</span> buffer = Buffer.concat([buffer1, buffer2]);</span><br><span class="line"><span class="built_in">console</span>.log(buffer.toString());</span><br></pre></td></tr></table></figure>

<ul>
<li>判断是否是buffer</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">Buffer.isBuffer();</span><br></pre></td></tr></table></figure>

<ul>
<li>获取字节长度(显示是字符串所代表buffer的长度)</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> str = <span class="string">&#x27;珠峰&#x27;</span>;</span><br><span class="line"><span class="built_in">console</span>.log(str.length);</span><br><span class="line"><span class="keyword">let</span> buffer = Buffer.from(str);</span><br><span class="line"><span class="built_in">console</span>.log(Buffer.byteLength(buffer));</span><br></pre></td></tr></table></figure>

<h2 id="base64"><a href="#base64" class="headerlink" title="base64"></a>base64</h2><ul>
<li>Base64是网络上最常见的用于传输8Bit字节码的编码方式之一</li>
<li>Base64就是一种基于64个可打印字符来表示二进制数据的方法</li>
<li>Base64要求把每三个8Bit的字节转换为四个6Bit的字节（3 _8 = 4_6 = 24），然后把6Bit再添两位高位0，组成四个8Bit的字节</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> CHARTS = <span class="string">&#x27;ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/&#x27;</span>;</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">transfer</span>(<span class="params">str</span>)</span>&#123;</span><br><span class="line">  <span class="keyword">let</span> buf = Buffer.from(str);</span><br><span class="line">  <span class="keyword">let</span> result = <span class="string">&#x27;&#x27;</span>;</span><br><span class="line">  <span class="function"><span class="title">for</span>(<span class="params"><span class="keyword">let</span> b <span class="keyword">of</span> buf</span>)</span>&#123;</span><br><span class="line">      result += b.toString(<span class="number">2</span>);</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="keyword">return</span> result.match(<span class="regexp">/(\d&#123;6&#125;)/g</span>).map(<span class="function"><span class="params">val</span>=&gt;</span><span class="built_in">parseInt</span>(val,<span class="number">2</span>)).map(<span class="function"><span class="params">val</span>=&gt;</span>CHARTS[val]).join(<span class="string">&#x27;&#x27;</span>);</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">let</span> r = transfer(<span class="string">&#x27;珠&#x27;</span>);</span><br><span class="line"><span class="built_in">console</span>.log(r);</span><br></pre></td></tr></table></figure>
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/kity@2.0.4/dist/kity.min.js"></script><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/kityminder-core@1.4.50/dist/kityminder.core.min.js"></script><script defer="true" type="text/javascript" src="https://cdn.jsdelivr.net/npm/hexo-simple-mindmap@0.2.0/dist/mindmap.min.js"></script><link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/hexo-simple-mindmap@0.2.0/dist/mindmap.min.css">
    </div>

    
    
    

    <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/Buffer/" rel="tag"># Buffer</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/2022/07/09/%E5%89%8D%E7%AB%AF%E8%AE%B0%E5%BD%95%E6%95%B4%E7%90%86/Encoding/" rel="prev" title="Encoding">
                  <i class="fa fa-chevron-left"></i> Encoding
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/2022/07/10/%E4%BB%A3%E7%A0%81%E4%B8%AD%E6%B3%A8%E6%84%8F%E9%97%AE%E9%A2%98/" rel="next" title="代码中注意问题">
                  代码中注意问题 <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 
  <span itemprop="copyrightYear">2023</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">郭泽</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/" rel="noopener" target="_blank">NexT.Gemini</a> 强力驱动
  </div>

    </div>
  </footer>

  
  <script src="https://cdnjs.cloudflare.com/ajax/libs/animejs/3.2.1/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/fancybox/3.5.7/jquery.fancybox.min.js" integrity="sha256-yt2kYMy0w8AbtF89WXb2P1rfjcP/HTHLT7097U8Y5b8=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/next-boot.js"></script><script src="/js/bookmark.js"></script>

  
<script src="https://cdnjs.cloudflare.com/ajax/libs/hexo-generator-searchdb/1.4.0/search.js" integrity="sha256-vXZMYLEqsROAXkEw93GGIvaB2ab+QW6w3+1ahD9nXXA=" crossorigin="anonymous"></script>
<script src="/js/third-party/search/local-search.js"></script>

  <script class="next-config" data-name="pdf" type="application/json">{"object_url":{"url":"https://cdnjs.cloudflare.com/ajax/libs/pdfobject/2.2.8/pdfobject.min.js","integrity":"sha256-tu9j5pBilBQrWSDePOOajCUdz6hWsid/lBNzK4KgEPM="},"url":"/lib/pdf/web/viewer.html"}</script>
  <script src="/js/third-party/tags/pdf.js"></script>

  <script class="next-config" data-name="mermaid" type="application/json">{"enable":true,"theme":{"light":"default","dark":"dark"},"js":{"url":"https://cdnjs.cloudflare.com/ajax/libs/mermaid/9.1.3/mermaid.min.js","integrity":"sha256-TIYL00Rhw/8WaoUhYTLX9SKIEFdXxg+yMWSLVUbhiLg="}}</script>
  <script src="/js/third-party/tags/mermaid.js"></script>

  <script src="/js/third-party/fancybox.js"></script>


  





</body>
</html>
